# coding: utf-8
from PySide6.QtCore import QThread, Signal
from ..utils import QFileManager
from ..file import Files


class VoiceThread(QThread):
    voiceChanged = Signal(list)
    languageChanged = Signal(list)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.key = 'zh-CN'
        self.languages = []

    def run(self):
        with QFileManager(Files.EDGE_TTS) as file:
            data = file.read_json()
            languages = list(data.keys())
            self.languages = languages
            self.languageChanged.emit(languages)
            voices = data.get(self.key, [])
            self.voiceChanged.emit(voices)

    def setKey(self, key: str = 'zh-CN'):
        self.key = key
        self.start()
